package com.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author likz
 * @Date 2025/3/28 9:20
 * @Description
 **/
public class ServerTest {

    public static void main(String[] args) {
        int port = 65525; // 服务器监听的端口号
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("服务器启动，等待客户端连接...");
            Socket clientSocket = serverSocket.accept(); // 接受客户端连接
            System.out.println("客户端已连接");

            // 从客户端读取数据
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // 向客户端发送数据
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("收到客户端消息: " + inputLine);
                // 可以在这里处理接收到的数据

                // 发送响应给客户端
                out.println("服务器已收到消息: " + inputLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
